/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.inputQuestion;

import ru.irenproject.Action;
import ru.irenproject.Check;
import ru.irenproject.TestInputException;
import ru.irenproject.Visual;
import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Event;
import ru.irenproject.store.Thing;

import com.google.common.primitives.Ints;

public abstract class AbstractPatternEditor extends AbstractThing
    implements PatternEditor, Visual {
  public static final class SetValueInput {
    public String value;
  }

  public static final class SetQualityInput {
    public String quality;
  }

  protected abstract void setPattern(Pattern pattern);

  @Action(badInput = TestInputException.class)
  public final void doSetValue(SetValueInput in) {
    pattern().setValue(Check.inputNotNull(in.value));
  }

  @Action(badInput = TestInputException.class)
  public final void doSetQuality(SetQualityInput in) {
    Integer percent = Ints.tryParse(Check.inputNotNull(in.quality).trim());
    pattern().setQualityPercent(Check.inputNotNull(percent));
  }

  @Override protected final void handleEvent(Event e, Thing source) {
    if (e instanceof AbstractPattern.ChangeEvent) {
      markForRender();
    }
  }
}
